<!---

Copyright 2008:

Author: Marco van den Oever
Company: WSO
Website: http://www.webserviceonline.biz
Email: marco@webserviceonline.biz

License: 

This software is licensed under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
For a copy of the GNU General Public License check the license folder or see <http://www.gnu.org/licenses/>.

Description:

wsoFarcryPolling, a Farcry plugin to show pollings on a Farcry based project

Build with great support from the Farcry-Dev group! (Google on Farcry-Dev)

Created: 18/10/08

Tested on CF8

--->
<cfsetting enablecfoutputonly="true" />
<cfimport taglib="/farcry/core/tags/formtools" prefix="ft" />
<cfimport taglib="/farcry/core/tags/webskin" prefix="skin" />
<!--- get the pollingdata --->
<cfset obj = createObject('component',application.stcoapi.polling.typePath) />
<cfset stprops_pollingdata = obj.getData(stobj.pollingobjectid) />
<!--- set stprops_pollingdata.objectid in valid variable name for use as object identifier --->
<cfset pollingid = replace(stprops_pollingdata.objectid,'-','','ALL') />
<!--- set application['wsoFarcryPolling' & pollingid] to set db question count --->
<cfset application['wsoFarcryPolling' & pollingid] = listLen(stprops_pollingdata.answers) />
<!--- check if voter allready voted for this poll --->
<cfset obj = createObject('component',application.stcoapi.pollingvotes.typePath) />
<cfset stprops = {} />
<cfset stprops.conditions.userip=cgi.remote_host />
<cfset stprops.conditions.pollingobjectid=stobj.pollingobjectid />
<cfset votecheck = obj.getMultipleByQuery(argumentCollection=stprops) />
<cfif votecheck.recordcount GT 0>
  <cfset voteranswer = listGetAt(stprops_pollingdata.answers, #votecheck.answer#)>
</cfif>
<!--- if the ip address of the voter not yet exists enable processform and poll form, else show polling statistics --->

<cfif not len(votecheck.userip)>
  <!--- Process poll --->
  <ft:processform action="#stprops_pollingdata.submitlabel#">
    <cfif structkeyexists(form,stprops_pollingdata.objectid)>
      <!--- set the polling vote --->
      <cfset stprops = {} />
      <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
        <cfif #form[stprops_pollingdata.objectid]# EQ i>
          <cfset variables.stprops.answer = i />
        </cfif>
      </cfloop>
      <cfset stprops.pollingobjectid = stprops_pollingdata.objectid>
      <cfset stprops.userip = cgi.remote_host>
      <cfset obj.createData(stProperties=stprops) />
      <!--- redirect so the vote statistics are shown --->
      <cflocation url="#application.url.webRoot#?objectid=#url.objectid#" addtoken="no">
    </cfif>
  </ft:processform>
  <!--- show the poll form --->
  <ft:form> <cfoutput>
      <table>
        <tr>
          <td colspan="2" align="left">#stprops_pollingdata.question#</td>
        </tr>
        <tr>
          <td colspan="2" align="left">&nbsp;</td>
        </tr>
        <cfloop index="i" from="1" to="#application['wsoFarcryPolling' & pollingid]#">
          <cfif listGetAt(stprops_pollingdata.answers, #i#) GTE 0>
            <tr>
              <td align="left"><li>
                  <cfset currentanswer = listGetAt(stprops_pollingdata.answers, #i#)>
                  #currentanswer#</li></td>
              <td align="left"><input type="radio" name="#stprops_pollingdata.objectid#" value="#i#" id="#stprops_pollingdata.objectid#_#i#" /></td>
            </tr>
          </cfif>
        </cfloop>
        <tr>
          <td colspan="2" align="left">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2" align="left"><ft:button value="#stprops_pollingdata.submitlabel#" size="small" /></td>
        </tr>
        <tr>
          <td colspan="2" align="left">&nbsp;</td>
        </tr>
      </table>
    </cfoutput> </ft:form>
  <cfelse>
  <!--- get the polling data stats --->
  <cfset obj = createObject('component',application.stcoapi.pollingvotes.typePath) />
  <!--- set default answer_sum so calculate can be done --->
  <cfset answers_sum = 0>
  <cfloop index="i" from="1" to="#listLen(stprops_pollingdata.answers)#">
    <cfset props = {} />
    <cfif listGetAt(stprops_pollingdata.answers, #i#) GT 0>
      <cfset props.conditions.answer=#i# />
    </cfif>
    <cfset props.conditions.pollingobjectid=stprops_pollingdata.objectid />
    <cfset variables['answersum' & i] = obj.getMultipleByQuery(argumentCollection=props) />
    <cfif variables['answersum' & i].recordcount GT 0>
      <cfset variables['ansum' & i] = variables['answersum' & i].recordcount>
      <!--- get the total votes of all answers that exists --->
      <cfset answers_sum = answers_sum + variables['ansum' & i]>
    </cfif>
  </cfloop>
  <cfoutput>
    <!--- show the polling results --->
    <table>
      <tr>
        <td colspan="2" align="left">#stprops_pollingdata.question#</td>
      </tr>
      <tr>
        <td colspan="2" align="left">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" align="left"><!--- show the polling results in cfchart --->
          <cfchart format="png"
chartwidth="300" chartheight="300" showlegend="yes" show3d="yes">
          <cfchartseries type="pie">
          <cfloop index="i" from="1" to="#listLen(stprops_pollingdata.answers)#">
            <cfif variables['answersum' & i].recordcount GT 0>
              <cfset answer = listGetAt(stprops_pollingdata.answers, #i#) />
              <cfchartdata item="#answer# (#variables['ansum' & i]#)" value="#variables['ansum' & i]#">
			  <cfelse>
			  <cfset answer = listGetAt(stprops_pollingdata.answers, #i#) />
              <cfchartdata item="#answer# (0)" value="0">
            </cfif>
          </cfloop>
          <cfchartdata item="Total votes (#answers_sum#)" value="#answers_sum#">
          </cfchartseries>
          </cfchart>
        </td>
      </tr>
    </table>
  </cfoutput>
</cfif>
<cfsetting enablecfoutputonly="false" />
